-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/6/1
-- @module:   map
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class map
local map = {}
local this = map

local import = import
local map_ctx = map_ctx
local map_unit = map_unit
---@type map_cfg
local map_cfg = import("game/config/map_cfg")
local MAP_INFO = map_cfg.MAP_INFO
local HUNT_INFO = map_cfg.HUNT_INFO

-- 得到地图配置
function this.get_map_config_by_id(id)
    local ret = nil
    for k, v in pairs(MAP_INFO) do
        if v.map_id == id then
            v.mapName = k
            ret = v
            break
        end
    end
    return ret
end

-- 得到地图配置
function this.get_map_config_by_name(name)
    local ret = MAP_INFO[name]
    return ret
end

-- 得到传送点
function this.get_transfer_id_by_map_name(map_name)
    -- 获取传送地图的信息表
    local map_list = this.get_map_list()
    -- 遍历所有地图的传送信息表
    for _, map_info in pairs(map_list) do
        -- 如果传送的地图名相同
        if map_name == map_info.name then
            -- 返回传送id
            return map_info.id
        end
    end
end

-- 得到传送点
function this.get_transfer_id_by_mapId(mapId)
    local mapConfig = this.get_map_config_by_id(mapId)
    if not mapConfig then
        return nil
    end
    local map_name = mapConfig.mapName
    -- 获取传送地图的信息表
    local map_list = this.get_map_list()
    -- 遍历所有地图的传送信息表
    for _, map_info in pairs(map_list) do
        -- 如果传送的地图名相同
        if map_name == map_info.name then
            -- 返回传送id
            return map_info.id
        end
    end
    return -1
end


-- 得到地图列表
function this.get_map_list()
    local list = map_unit.list()
    local ret = {}
    for i = 1, #list do
        local obj = list[i]
        if map_ctx:init(obj) then
            table.insert(ret, {
                obj = obj,
                id = map_ctx:id(),
                name = map_ctx:name()
            })
        end
    end
    return ret
end

-- 得到挂机地图数据
function this.get_hunt_data(owner)
    local ret = nil
    local level = owner.level
    for _, v in ipairs(HUNT_INFO) do
        if level >= v.minLevel and level < v.maxLevel then
            if not ret then
                ret = {}
            end
            table.insert(ret, v)
        end
    end
    return ret
end

-- 通过主地图ID找挂机点
function this.get_hunt_pos_data_by_curMainMapId(owner)
    local ret = nil
    local level = owner.level
    local curMainMapId = owner.mainMapId
    for _, v in ipairs(HUNT_INFO) do
        if level >= v.minLevel then
            for _, posData in ipairs(v.areaArr) do
                if posData.main_map_id == curMainMapId then
                    if not ret then
                        ret = {}
                    end
                    table.insert(ret, posData)
                end
            end
        end
    end
    return ret
end

return this